package dao.impl;

import dao.EmpDao;
import pojo.Emp;
import util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class EmpDaoImpl implements EmpDao {
    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;
    @Override
    public List<Emp> findEmps() {
        List<Emp> emps = new ArrayList<>();
        Emp emp = null;
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement("select * from emp");
            rs = pstmt.executeQuery();
            while (rs.next()){
                emp = new Emp(rs.getInt(1),rs.getString(2),rs.getString(3), rs.getInt(4),
                        rs.getDate(5),rs.getDouble(6),rs.getDouble(7),rs.getInt(8));
                emps.add(emp);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(rs,pstmt,conn);
        }
        return emps;
    }

    @Override
    public String deleteEmpByEmpno(Integer empno) {
        try {
            conn = DruidUtil.getConnection();
            pstmt = conn.prepareStatement("delete from emp where empno=?");
            pstmt.setInt(1,empno);
            pstmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DruidUtil.closeAll(null,pstmt,conn);
        }
        return empno+"删除成功";
    }
}
